import 'package:flutter/material.dart';
import 'package:kq_flutter_core_widget/utils/kq_screen_util.dart';
import 'package:kq_flutter_tv_widgets/resources/images.dart';
import '../../config/kq_global.dart';
import '../../resources/kq_theme_colors.dart';
import '../../resources/l10n/kq_intl.dart';
import 'package:kq_flutter_core_widget/widgets/button/kq_ink_well.dart';
import '../../theme/kq_theme_manager.dart';
import '../tabView/kq_tab_view.dart';

class KqChooseDialog extends Dialog {
  //标题
  final String title;

  /// 数据源
  final List<KqChooseData> items;

  ///选中的数据
  final List<String>? chosenIds;

  //是否是多选
  final bool isSingleChoose;

  void Function(void Function()) state;

  /// 回调
  final Function(List<KqChooseData> chooses)? callback;

  KqChooseDialog(
      {Key? key,
      required this.title,
      required this.items,
      required this.state,
      this.chosenIds,
      this.isSingleChoose = true,
      this.callback})
      : super(key: key);

  /// 获取选中的数据
  List<KqChooseData> getChosenData() {
    var chosenData = <KqChooseData>[];
    if (items.isNotEmpty) {
      for (var element in items) {
        if (element.chosen) {
          chosenData.add(element);
        }
      }
    }
    return chosenData;
  }

  ///多选的
  List<Widget> _renderMore() {
    List<Widget> children = <Widget>[];
    for (int i = 0; i < items.length; i++) {
      var item = items[i];
      Color bgColor = KqThemeColors.bgBlack05;
      Color txtColor =
          KqThemeManager.instance.getConfig().dialogConfig.titleColor ??
              KqThemeColors.textBlack65;
      Color borderColor = KqThemeColors.bgTransparent;

      if ((chosenIds != null && chosenIds!.isNotEmpty)) {
        if (chosenIds!.contains(item.itemId)) {
          item.chosen = true;
        }
      }
      if (item.chosen) {
        bgColor = KqThemeColors.bgWhite;
        borderColor = KqThemeColors.bgBlue;
        txtColor = KqThemeColors.textBlue;
      }
      children.add(KqInkWell(
        onTap: () {
          //多选
          state(() {
            item.chosen = !item.chosen;
          });
        },
        child: Stack(
          children: [
            Container(
              decoration: BoxDecoration(
                borderRadius: BorderRadius.circular(2.r),
                border: Border.all(width: 1.75.r, color: borderColor),
                color: bgColor,
              ),
              alignment: Alignment.center,
              padding: EdgeInsets.symmetric(horizontal: 8.r),
              child: Text(
                item.name ?? '',
                maxLines: 2,
                overflow: TextOverflow.ellipsis,
                textAlign: TextAlign.center,
                style: TextStyle(color: txtColor, fontSize: 24.sp),
              ),
            ),
            if (item.chosen)
              Positioned(
                  bottom: 0,
                  right: 0,
                  child: Image.asset(
                    Images.commonIcXuanzhong,
                    width: 40.r,
                    height: 35.r,
                    package: KqGlobal.packageName,
                  ))
          ],
        ),
      ));
    }
    return children;
  }

//单选的
  List<Widget> _renderSingle() {
    List<Widget> children = <Widget>[];
    for (int i = 0; i < items.length; i++) {
      var item = items[i];
      Color bgColor = KqThemeColors.bgBlack05;
      Color txtColor =
          KqThemeManager.instance.getConfig().dialogConfig.titleColor ??
              KqThemeColors.textBlack65;

      if ((chosenIds != null && chosenIds!.isNotEmpty)) {
        if (chosenIds!.contains(item.itemId)) {
          item.chosen = true;
        }
      }
      if (item.chosen) {
        bgColor = KqThemeColors.bgBlue;
        txtColor = KqThemeColors.textWhite;
      }
      children.add(KqInkWell(
        onTap: () {
          //单选
          if (callback != null) {
            callback!([item]);
          }
        },
        child: Container(
          decoration: BoxDecoration(
            borderRadius: BorderRadius.circular(2.r),
            color: bgColor,
          ),
          alignment: Alignment.center,
          padding: EdgeInsets.symmetric(horizontal: 8.r),
          child: Text(
            item.name ?? '',
            maxLines: 2,
            overflow: TextOverflow.ellipsis,
            textAlign: TextAlign.center,
            style: TextStyle(color: txtColor, fontSize: 24.sp),
          ),
        ),
      ));
    }
    return children;
  }

  @override
  Widget build(BuildContext context) {
    Widget dialogChild = Column(
      mainAxisSize: MainAxisSize.min,
      crossAxisAlignment: CrossAxisAlignment.center,
      children: [
        Container(
          padding: EdgeInsets.all(20.r),
          alignment: Alignment.center,
          child: Text(
            title,
            maxLines: 1,
            overflow: TextOverflow.ellipsis,
            style: TextStyle(fontSize: 28.sp, color: KqThemeColors.textBlack85),
          ),
        ),
        SizedBox(
          height: 8.r,
        ),
        Expanded(
          child: GridView.count(
            shrinkWrap: true,
            padding: EdgeInsets.symmetric(horizontal: 32.r),
            crossAxisSpacing: 16.r,
            mainAxisSpacing: 16.r,
            crossAxisCount: 5,
            childAspectRatio: 17 / 6.0,
            children: isSingleChoose ? _renderSingle() : _renderMore(),
          ),
        ),
        SizedBox(
          height: 16.r,
        ),
        if (!isSingleChoose)
          KqTabView(
            titles: [
              KqIntl.currentResource.cancel,
              KqIntl.currentResource.confirm
            ],
            colorTypes: const [ColorType.lineBlue, ColorType.fillBlue],
            tabIndexBlock: (index, disable) {
              if (index == 0) {
                Navigator.pop(context);
              }
            },
          )
      ],
    );
    return UnconstrainedBox(
        child: SizedBox(
            width: 1148.r,
            child: Material(
                shape: RoundedRectangleBorder(
                    borderRadius: BorderRadius.all(Radius.circular(4.r))),
                color:
                    KqThemeManager.instance.getConfig().dialogConfig.bgColor ??
                        KqThemeColors.radioUnCheckBgColor,
                child: LimitedBox(
                  maxHeight: 836.r,
                  child: ClipRRect(
                    borderRadius: BorderRadius.all(Radius.circular(4.r)),
                    child: dialogChild,
                  ),
                ))));
    ;
  }
}

class KqChooseData {
  /// 选项标题
  final String? name;

  /// 选项id，非必填
  final String? itemId;

//是否选中
  bool chosen;

  KqChooseData({required this.name, this.itemId, this.chosen = false});
}
